package org.yumeko.producer;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.support.MessageBuilder;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;

//生产者分同步 异步消息
//类型分普通 延迟/定时 有序 事务
@Configuration
public class MessageProducer implements CommandLineRunner{
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void run(String... args) {
        send();
    }
    //发送同步消息的不同方式
    public void send() {
//        //发送spring消息
        rocketMQTemplate.send("TopicZero", MessageBuilder.withPayload("zero Hello, World!").build());
//        //发送spring同步消息
//        rocketMQTemplate.convertAndSend("TopicOne", "one Hello, World!");
//        //发送spring同步消息
//        rocketMQTemplate.convertAndSend("TopicTwo", "two Hello, World!", Map.of("headerKey", "headerValue"));
//        //发送spring同步消息
//        rocketMQTemplate.convertAndSend("TopicThree","three Hello, World!", message -> {
//                    // 直接使用MessageBuilder来构建消息
//                    return MessageBuilder.fromMessage(message)
//                            .setHeader(RocketMQHeaders.KEYS, "keysThree")
//                            .build();
//                }
//        );
//        //发送rocketmq同步消息
//        rocketMQTemplate.syncSend("TopicFour", MessageBuilder.withPayload("four Hello, World!").setHeader("TAGS", "four").setHeader("KEYS", "four").build(),5000,0);
//        //发送rocketmq同步有序消息
//        rocketMQTemplate.syncSendOrderly("TopicFive", MessageBuilder.withPayload("five Hello, World!").build(),"order_001");
//        //发送rocketmq同步延迟消息(秒)
//        rocketMQTemplate.syncSendDelayTimeSeconds("TopicSix", MessageBuilder.withPayload("six Hello, World!").build(), 30);
//        //发送rocketmq同步延迟消息(毫秒)
//        rocketMQTemplate.syncSendDelayTimeMills("TopicSeven", MessageBuilder.withPayload("seven Hello, World!").build(), 30000);
//        //发送rocketmq同交付时间内发送消息(毫秒)
//        rocketMQTemplate.syncSendDeliverTimeMills("TopicEight", MessageBuilder.withPayload("eight Hello, World!").build(), 3000);
//        //发送rocketmq异步消息
//        rocketMQTemplate.asyncSend("TopicNine", new OrderPaidEvent("nine_order_001", new BigDecimal("88.00")), new SendCallback() {
//            @Override
//            public void onSuccess(SendResult var1) {
//                System.out.printf("async on Success SendResult=%s %n", var1);
//            }
//
//            @Override
//            public void onException(Throwable var1) {
//                System.out.printf("async on Exception Throwable=%s %n", var1);
//            }
//        },3000);
//        //发送rocketmq异步有序消息
//        rocketMQTemplate.syncSendOrderly("TopicTen", MessageBuilder.withPayload("Ten Hello, World!")
//                .setHeader("TAGS", "orderKey").build(), "order_001",3000,0);
//        //发送rocketmq单项消息
//        rocketMQTemplate.sendOneWay( "TopicEleven", MessageBuilder.withPayload("Eleven Hello, World!").build());
//        //发送rocketmq单项有序消息
//        rocketMQTemplate.sendOneWayOrderly( "TopicTwelve", MessageBuilder.withPayload("Twelve Hello, World!").build(), "Twelve_order_001");
//        //发送rocketmq事务消息
//        rocketMQTemplate.sendMessageInTransaction( "TopicThirteen", MessageBuilder.withPayload(new OrderPaidEvent("Thirteen_order_001", new BigDecimal("88.00"))).build(), null);
//        // 同步发送request并且等待String类型的返回值
//        String replyString = rocketMQTemplate.sendAndReceive("stringRequestTopic", "request string", String.class);
//        System.out.printf("send %s and receive %s %n", "request string", replyString);
//        // 异步发送request并且等待User类型的返回值
//        rocketMQTemplate.sendAndReceive("objectRequestTopic", new OrderPaidEvent("Thirteen_order_001", new BigDecimal("88.00")), new RocketMQLocalRequestCallback<OrderPaidEvent>() {
//            @Override
//            public void onSuccess(OrderPaidEvent message) {
//                System.out.printf("send user object and receive %s %n", message.toString());
//            }
//
//            @Override public void onException(Throwable e) {
//                e.printStackTrace();
//            }
//        }, 5000);

    }
//    @Slf4j
//    @Component
//    @RocketMQTransactionListener
//    static class MyConsumerThirteen implements RocketMQLocalTransactionListener {
//        @Override
//        public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
//            return RocketMQLocalTransactionState.UNKNOWN;
//        }
//
//        @Override
//        public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
//            return RocketMQLocalTransactionState.COMMIT;
//        }
//    }
    @Data
    @AllArgsConstructor
    static class OrderPaidEvent implements Serializable {
        private String orderId;
        private BigDecimal paidMoney;
    }

}
